package com.woniu.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.IdentityInformationDto;
import com.woniu.exception.user.UserException;
import com.woniu.form.InsertAdminForm;
import com.woniu.form.InsertUserOrStoreForm;
import com.woniu.form.LoginForm;
import com.woniu.form.NameLoginForm;
import com.woniu.param.InsertAdminParam;
import com.woniu.param.InsertUserOrStoreParam;
import com.woniu.param.LoginParam;
import com.woniu.user.service.UserService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    UserService userService;

    //用户名登录（管理员）
    @PostMapping("/loginAdminByName.api")
    @ApiOperation("用户名登录（管理员）")
    public Result<IdentityInformationDto> loginAdminByName(@Valid @RequestBody NameLoginForm nameLoginForm, HttpServletResponse response) throws Exception {
        IdentityInformationDto identityInformationDto = userService.loginAdminByName(nameLoginForm);
        Map<String, Object> map = BeanUtil.beanToMap(identityInformationDto.getAdmin());


        //调API生成jwt令牌
        String token = JwtUtil.createToken(map, 600 * 12);
        //通过响应头将jwt响应给客户端
        response.setHeader("token", token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(IdentityInformationDto.class).setData(identityInformationDto).setMsg("管理员登录成功").setCode(200);

    }

    //管理员登录（管理员，超级管理员）
    @PostMapping("/loginAdmin.api")
    @ApiOperation("管理员登录")
    public Result<IdentityInformationDto> loginAdmin(@Valid @RequestBody LoginForm loginForm, HttpServletResponse response) throws Exception {
        IdentityInformationDto identityInformationDto = userService.loginRequire(BeanUtil.toBean(loginForm, LoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(identityInformationDto.getAdmin());


        //调API生成jwt令牌
        String token = JwtUtil.createToken(map, 600 * 12);
        //通过响应头将jwt响应给客户端
        response.setHeader("token", token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(IdentityInformationDto.class).setData(identityInformationDto).setMsg("管理员登录成功").setCode(200);

    }


    //新增管理员
    @PostMapping("/insertAdmin")
    @ApiOperation("新增管理员账号")
    @RequireRole("超级管理员")
    public Result insertAdmin(@Valid @RequestBody InsertAdminForm insertAdminForm, @RequestHeader("token") String token) throws Exception {


        Claims claims = JwtUtil.parseToken(token);
        String operatorUserName = claims.get("adminName").toString();

        InsertAdminParam insertAdminParam = BeanUtil.toBean(insertAdminForm, InsertAdminParam.class);
        insertAdminParam.setOperator(operatorUserName);
        userService.insertAdmin(insertAdminParam);
        return Result.getResult().setMsg("新增管理员成功").setCode(200);

    }


//    新增学生账号或维修员或商家账号
    @PostMapping("/insertUser.api")
    @ApiOperation("新增学生账号或维修员或商家账号")
    @RequireRole("超级管理员")
    @RequirePermission("新增人员")
    public Result insertUserOrStore(@Valid @RequestBody InsertUserOrStoreForm insertUserOrStoreForm, @RequestHeader("token") String token) throws Exception {

        Claims claims = JwtUtil.parseToken(token);
        String operatorUserName = null;
        try {
            operatorUserName = claims.get("adminName").toString();
        } catch (Exception e) {
            throw new UserException("非管理员不得新增账号", 40003);
        }

        InsertUserOrStoreParam insertUserOrStoreParam = BeanUtil.toBean(insertUserOrStoreForm, InsertUserOrStoreParam.class);
        insertUserOrStoreParam.setOperatorUserName(operatorUserName);
        userService.insertUserOrFixer(insertUserOrStoreParam);
        return Result.getResult().setMsg("新增成功").setCode(200);

    }


}

